<?php

namespace App\Console\Commands;

use App\Models\Goods;
use App\Models\GoodsSku;
use App\Models\Order;
use App\Models\SeckillGoods;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class ReidsSubscribe extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:auto_clear';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单过期处理发布订阅配置成功';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $pattern = '__keyevent@' . 0 . '__:expired';
        Redis::subscribe([$pattern], function ($message) {
            echo $message;

            $arr = explode(':',$message);

            //查询是哪种类型的过期
            switch ($arr['0']) {
            	case 'preparation':
                    //预订单过期 补回库存给队列
                    \Log::info('预下单过期：' . json_encode($arr));
                    $redis = new \Redis();
                    $redis->connect('127.0.0.1','6379');
                    $redis->auth('myredis');

                    //出队
                    $redis->rpop('dec_seckill_goods_stock:goods_id:'.$arr['4'].':active_id:'.$arr['8']);

                    //判断活动是否已经过期
                    $seckill = SeckillGoods::query()->where([
                        'id'=>$arr['8'],
                        'kill_status'=>1
                    ])->first();

                    if($seckill){
                        //补回销售队列
                        $key = 'seckill_goods_stock:goods_id:' . $arr['4'].':active_id:'.$arr['8'];
                        $redis->lpush($key,1);

                        //补回hash表中
                        $goods_sku_number_key = 'goods_sku_stock:goods_id:'.$arr['4'].':active_id:'.$arr['8'];
                        $goods_number = $redis->hget($goods_sku_number_key,$arr['6']);
                        $redis->hset($goods_sku_number_key,$arr['6'],++$goods_number);

                        $expired_time = strtotime($seckill['end_time']) - time();
                        //设置key的过期时间
                        $redis->expire($key, $expired_time);
                        $redis->expire($goods_sku_number_key, $expired_time);
                    }

            		break;
                case 'create_sckill_order':
                    $redis = new \Redis();
                    $redis->connect('127.0.0.1','6379');
                    $redis->auth('myredis');
                    //已订单，未付款过期 补回库存给队列 并删除订单
                    //预订单过期 补回库存给队列
                    \Log::info('已下单过期：' . json_encode($arr));
                    //出队
//                    $redis->rpop('dec_seckill_goods_stock:goods_id:'.$arr['1'].':sku:'.$arr['5']);
                    //补回销售队列

                    //判断活动是否已经过期
                    $seckill = SeckillGoods::query()->where([
                        'id'=>$arr['9'],
                        'kill_status'=>1
                    ])->first();

                    if($seckill){
                        //补回销售队列
                        $key = 'seckill_goods_stock:goods_id:' . $arr['1'].':active_id:'.$arr['9'];
                        $redis->lpush($key,1);

                        //补回hash表中
                        $goods_sku_number_key = 'goods_sku_stock:goods_id:'.$arr['1'].':active_id:'.$arr['9'];
                        $goods_number = $redis->hget($goods_sku_number_key,$arr['5']);
                        $redis->hset($goods_sku_number_key,$arr['5'],++$goods_number);

                        $expired_time = strtotime($seckill['end_time']) - time();
                        //设置key的过期时间
                        $redis->expire($key, $expired_time);
                    }

                    //删除订单 并还原库存
                    $order = Order::query()->where([
                            'order_sn'=>$arr['7'],
                            'status'=>0
                        ])->first();

                    if($order){
                        $order->orderInfo()->delete();
                        $order->delete();
                    }

                    //秒杀表
                    SeckillGoods::query()->where([
                        'goods_id'=>$arr['1'],
                        'id'=>$arr['9'],//活动对应的id
                    ])->decrement('sale_num',1);
                    //库存表
                    GoodsSku::query()->where([
                        'goods_id'=>$arr['1'],
                        'sku_list'=>$arr['5']
                    ])->increment('inventory',1);

                    //商品主表
                    Goods::query()->where(['id'=>$arr['1']])->increment('inventory',1);
                    //销量 -1
                    Goods::query()->where(['id'=>$arr['1']])->decrement('sales_num',1);
                    break;

            	default:
            		# code...
            		break;
            }

            \Log::info('redis订阅日志:'.$message);
        });
    }
}
